﻿using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _AlterarCandidato : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["cpf"] == null)
            Response.Redirect("default.aspx");
        else
        {
            if (!Page.IsPostBack)
            {
                SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
                SqlCommand consultaInscrito = null;
                SqlDataReader drConsultaInscrito = null;
                try
                {
                    conexaoIntranet.Open();
                    string instrucao = "SELECT * FROM CamaraInscritos WHERE CPF = '" + Session["cpf"].ToString() + "'";
                    consultaInscrito = new SqlCommand(instrucao, conexaoIntranet);
                    drConsultaInscrito = consultaInscrito.ExecuteReader();
                    if (drConsultaInscrito.HasRows)
                    {
                        drConsultaInscrito.Read();
                        txtEndereco.Text = drConsultaInscrito["Endereco"].ToString();
                        txtNumero.Text = drConsultaInscrito["Numero"].ToString();
                        txtComplemento.Text = drConsultaInscrito["Complemento"].ToString();
                        txtBairro.Text = drConsultaInscrito["Bairro"].ToString();
                        txtCidade.Text = drConsultaInscrito["Cidade"].ToString();
                        txtCEP.Text = drConsultaInscrito["CEP"].ToString();
                        ddEstadoCivil.SelectedIndex = ddEstadoCivil.Items.IndexOf(ddEstadoCivil.Items.FindByValue(drConsultaInscrito["EstadoCivil"].ToString()));
                        txtFone.Text = drConsultaInscrito["Telefone"].ToString();
                        txtFone2.Text = drConsultaInscrito["Telefone2"].ToString(); 
                        txtEmail.Text = drConsultaInscrito["Email"].ToString();
                        txtSenha.Text = drConsultaInscrito["Senha"].ToString();
                        txtSenhaConfirma.Text = drConsultaInscrito["Senha"].ToString();
                        drConsultaInscrito.Close();
                    }
                }
                finally
                {
                    conexaoIntranet.Close();
                }
            }
        }
    }
    protected void btnCadastro_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
        SqlCommand alteraInscrito = null;
        try
        {
            conexaoIntranet.Open();
            string instrucao = "UPDATE CamaraInscritos SET Endereco = '" + txtEndereco.Text.ToUpper() + "', Numero = '" + txtNumero.Text + "', Complemento = '" + txtComplemento.Text.ToUpper() + "', Bairro = '" + txtBairro.Text.ToUpper() + "', Cidade = '" + txtCidade.Text.ToUpper() + "', CEP = '" + txtCEP.Text + "', EstadoCivil = '"+ ddEstadoCivil.SelectedValue +"', Telefone = '" + txtFone.Text + "', Telefone2 = '" + txtFone2.Text + "', Email = '" + txtEmail.Text.ToLower() + "', Senha = '" + txtSenha.Text + "' WHERE CPF = '" + Session["cpf"].ToString() + "'";
            alteraInscrito = new SqlCommand(instrucao, conexaoIntranet);
            alteraInscrito.ExecuteNonQuery();
            Response.Redirect("mensagensc.aspx?id=4");
        }
        finally
        {
            conexaoIntranet.Close();
        }
    }
    protected void txtCEP_TextChanged(object sender, EventArgs e)
    {
        string cep = txtCEP.Text.Replace(".", "").Replace("-", "");
        if (cep.Length == 8)
        {
            SiteBLL.CEPS api = new SiteBLL.CEPS();
            DataTable tabela = api.GetLogradouroPorCEP(txtCEP.Text, "cep", "pe");
            if (tabela.Rows.Count > 0)
            {
                DataRow linha = tabela.Rows[0];
                string endereco = linha["descricaologradouro"].ToString();
                int posicao = endereco.IndexOf(" - at");
                if (posicao > 0)
                    endereco = endereco.Substring(0, posicao);
                else
                {
                    posicao = endereco.IndexOf(" - de ");
                    if (posicao > 0)
                        endereco = endereco.Substring(0, posicao);
                }
                txtEndereco.Text = endereco.ToString().ToUpper();
                txtBairro.Text = linha["descricaobairro"].ToString().ToUpper();
                txtCidade.Text = linha["descricaocidade"].ToString().ToUpper();
                txtNumero.Text = "";
                txtComplemento.Text = "";
                txtNumero.Focus();
            }
        }

    }
}